import request from '../utils/request'

// 创建订单
export const createOrder = (data) => {
  return request({
    url: '/order/create',
    method: 'post',
    data
  })
}

// 获取订单列表
export const getOrderList = (params) => {
  return request({
    url: '/order/list',
    method: 'get',
    params: {
      status: params.status,
      page: params.page,
      pageSize: params.pageSize
    }
  })
}

// 删除订单
export const deleteOrder = (oid) => {
  return request({
    url: '/order/delete',
    method: 'delete',
    params: {
      oid
    }
  })
}

// 根据订单ID查询订单
export const getOrderById = (orderId) => {
  return request({
    url: `/order/status/${orderId}`,
    method: 'get'
  })
}

// 根据订单状态查询订单
export const getOrdersByStatus = (status) => {
  return request({
    url: '/order/status',
    method: 'get',
    params: {
      status
    }
  })
}

// 支付订单
export const payOrder = (orderId) => {
  return new Promise((resolve, reject) => {
    request({
      url: '/alipay/pay',
      method: 'get',
      headers: {
        'Content-Type': 'application/json'
      },
      params: {
        orderId
      },
      responseType: 'text'
    })
  })
}

// 立即下单
export const createOrderNow = (productId, number, address, recipientName, telephone) => {
  return request({
    url: '/order/create/now',
    method: 'post',
    data: {
      productId,
      number,
      address,
      recipient_name: recipientName,
      telephone
    }
  })
}

// 模拟商家发货
export const shipOrder = (orderId) => {
  return request({
    url: `/order/ship/${orderId}`,
    method: 'post'
  })
}

// 模拟已收货
export const finishOrder = (orderId) => {
  return request({
    url: `/order/finish/${orderId}`,
    method: 'post'
  })
}

// 检查用户是否购买过此商品（可以评论）
export const checkUserCanComment = (spuId) => {
  return request({
    url: '/order/orderId',
    method: 'get',
    params: {
      spuId
    }
  })
}


//后台
//根据时间单位获取销售趋势
export const getSalesTrend = (timeUnit) => {
  return request({
    url: '/order/jiang/salesTrend',
    method: 'get',
    params: {
      timeUnit
    }
  })
}
//获取最近的10条订单
export const getRecentOrderList = () => {
  return request({
    url: '/order/jiang/recentOrders',
    method: 'get',
  })
}
// 获取订单列表
export const getAllOrderList = () => {
  return request({
    url: '/order/jiang/list',
    method: 'get',
  })
}

// 根据订单ID查询订单
export const getOrder = (orderId) => {
  return request({
    url: '/order/jiang/detail',
    method: 'get',
    params: {
      orderId
    }
  })
}

// 平台后台发货
export const jiangShipOrder = (orderId) => {
  return request({
    url: `/order/jiang/ship`,
    method: 'post',
    params: {
      orderId
    }
  })
}

// 平台后台取消订单
export const jiangCancelOrder = (orderId) => {
  return request({
    url: `/order/jiang/cancel`,
    method: 'post',
    params: {
      orderId
    }
  })
}
